//
//  WritePersonInfoVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/11/28.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "WritePersonInfoVC.h"
#import "ComityInfoGatherVC.h"
#import "MyInfoTureNameCell.h"
#import "MyInfoNickNameCell.h"
#import "MyInfoUserIconCell.h"
#import "MyInfoCallCell.h"
#import "MyInfoCommityCell.h"
#import "MyHouseNumberCell.h"
#import <ZLPhotoBrowser/ZLPhotoActionSheet.h>
#import <LEEAlert/LEEAlert.h>
#import "SelectedListView.h"
#import "MyInfoFooterView.h"
@interface WritePersonInfoVC ()
@property (nonatomic, strong) NSMutableArray<UIImage *> *selectImages;

@property (nonatomic, strong) NSMutableArray<NSDictionary *> *dateArray;

/** <#note#> */
@property (nonatomic, strong) NSArray *commityNames;
/** <#note#> */
@property (nonatomic, strong) NSArray *roomsNumber;


@property (nonatomic, strong) MyInfoFooterView *footerView;
@end

@implementation WritePersonInfoVC


- (MyInfoFooterView *)footerView{
    
    if (!_footerView) {
        
    MyInfoFooterView *temp = (MyInfoFooterView *)[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MyInfoFooterView class]) owner:nil options:nil].firstObject;
        
        
    [temp.topBtn addTarget:self action:@selector(registerAcount) forControlEvents:UIControlEventTouchUpInside];
        _footerView = temp;
        
    }
    
    return _footerView;
}

- (void)registerAcount{
    
    
    
    [SVProgressHUD showInfoWithStatus:@"功能等待接入..."];
}

- (NSArray *)commityNames{
    
    if (!_commityNames) {
        _commityNames = @[@"七区两江汇",@"八区新外滩",@"九区菁华园",@"十区御华苑",@"十一区观澜郡",@"十二区花漫里"];
    }
    
    return _commityNames;
}

- (NSArray *)roomsNumber{
    if (!_roomsNumber) {
        _roomsNumber = @[@"0101",@"0102",@"0103",@"0104",@"0201",@"0202",@"0203",@"0204",@"0301",@"0302",@"0303",@"0304",@"0401",@"0402",@"0403",@"0404",@"0501",@"0502",@"0503",@"0504",@"0601",@"0602",@"0603",@"0604",@"0701",@"0702",@"0703",@"0704",@"0801",@"0802",@"0803",@"0804",@"0901",@"0902",@"0903",@"0904",@"1001",@"1002",@"1003",@"1004",@"1101",@"1102",@"1103",@"1104",@"1201",@"1202",@"1203",@"1204",@"1301",@"1302",@"1303",@"1304",@"1401",@"1402",@"1403",@"1404",@"1501",@"1502",@"1503",@"1504"];
    }
    
    
    return _roomsNumber;
}



- (NSMutableArray<NSDictionary *> *)dateArray{
    
    if (_dateArray == nil) {
    
        
        _dateArray = [[NSMutableArray alloc]init];
        
        for (NSString *commityName in self.commityNames) {
            NSMutableDictionary *mutDictionary = [[NSMutableDictionary alloc]init];
            mutDictionary[@"name"] = commityName;
            NSMutableArray<NSMutableDictionary *> *tempArray = [[NSMutableArray alloc]init];
            
            for (int j = 1; j < 30; j ++) {
                NSMutableDictionary *subDictionary = [[NSMutableDictionary alloc]init];
                subDictionary[@"name"] = [NSString stringWithFormat:@"%.2d栋",j];
                
                NSMutableArray *subArray = [[NSMutableArray alloc]init];
                
                for (NSString *roomNumber in self.roomsNumber) {
                    [subArray addObject:roomNumber];
                }
                subDictionary[@"sub"] = subArray;
                
                [tempArray addObject:subDictionary];
                
            }
            mutDictionary[@"sub"] = tempArray;
            
            [_dateArray addObject:mutDictionary];
            
        }
        
    }
    
    
    return _dateArray;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"填写个人信息";
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyInfoTureNameCell class]) bundle:nil] forCellReuseIdentifier:[MyInfoTureNameCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyInfoNickNameCell class]) bundle:nil] forCellReuseIdentifier:[MyInfoNickNameCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyInfoUserIconCell class]) bundle:nil] forCellReuseIdentifier:[MyInfoUserIconCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyInfoCallCell class]) bundle:nil] forCellReuseIdentifier:[MyInfoCallCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyInfoCommityCell class]) bundle:nil] forCellReuseIdentifier:[MyInfoCommityCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyHouseNumberCell class]) bundle:nil] forCellReuseIdentifier:[MyHouseNumberCell identifierStr]];
    
    
//    self.tableView.tableFooterView = [[UIView alloc]init];
    
    self.tableView.showsVerticalScrollIndicator = NO;
    
    
    self.tableView.tableFooterView = self.footerView;

    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(nextStepDidClick)];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    
    
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)nextStepDidClick{
    
    ComityInfoGatherVC *tempVC = [[ComityInfoGatherVC alloc]init];
    
    [self.navigationController pushViewController:tempVC animated:YES];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 7;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        
        MyInfoTureNameCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyInfoTureNameCell identifierStr]];
        
        cell.model = self.model;
        
        return cell;
    } else if (indexPath.row == 1){
        
        MyInfoCallCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyInfoCallCell identifierStr]];
        cell.model = self.model;

        return cell;
    }else if (indexPath.row == 2){
        MyInfoNickNameCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyInfoNickNameCell identifierStr]];
        
        cell.model = self.model;

        return cell;
    }else if (indexPath.row == 3){
        
        MyInfoCommityCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyInfoCommityCell identifierStr]];
        cell.model = self.model;

        
        return cell;
      
    }else if (indexPath.row == 4){
        
        MyHouseNumberCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyHouseNumberCell identifierStr]];
        
        cell.model = self.model;

        return cell;
    }else if (indexPath.row == 5){
   
        
        
        MyInfoUserIconCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyInfoUserIconCell identifierStr]];
        
        cell.model = self.model;

        return cell;
    }else{
        
        
        
        return [[UITableViewCell alloc]init];
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    
    if (indexPath.row == 0) {
        
        return 52;
    } else if (indexPath.row == 1){
        
       return 52;
    }else if (indexPath.row == 2){
       return 52;
    }else if (indexPath.row == 3){
       return 52;
    }else if (indexPath.row == 4){
      return 52;
    }else if (indexPath.row == 5){
      return 90;
    }else{
        
        
        return 0;
        
    }
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.view endEditing:YES];
    if (indexPath.row == 1) {
        [self selectSex];
    } else if (indexPath.row == 3){
        
        [self selectCommunity];

        
    }else if (indexPath.row == 4){
        [self selectDetailAdress];

    }else if (indexPath.row == 5){
        
        [self selectIcon];
    }else{
        
    }
    
    
}




- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [self.view endEditing:YES];
}


- (void)selectSex{
    
    SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
    
    view.isSingle = YES;
    
    
    
    view.array = @[[[SelectedListModel alloc] initWithSid:0 Title:@"男"] ,
                   [[SelectedListModel alloc] initWithSid:1 Title:@"女"]
                   ];
    
    MJWeakSelf;
    view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
        
        [LEEAlert closeWithCompletionBlock:^{
            
            
            weakSelf.model.sex = [array firstObject].title;
            
            [weakSelf.tableView reloadData];
            
            
        }];
        
    };
    
    [LEEAlert alert].config
    .LeeTitle(@"选择性别")
    .LeeItemInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeCustomView(view)
    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeClickBackgroundClose(YES)
    .LeeShow();
}

    



- (void)selectIcon{
    
    ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
    
    actionSheet.sender = self;
    actionSheet.navBarColor = themeColor;
    actionSheet.sortAscending = YES;
    actionSheet.allowSelectImage = YES;
    actionSheet.allowSelectGif = NO;
    actionSheet.allowSelectVideo = NO;
    actionSheet.allowSelectLivePhoto = NO;
    actionSheet.allowForceTouch = NO;
    actionSheet.allowEditImage = YES;
    actionSheet.allowTakePhotoInLibrary = YES;
    actionSheet.showSelectedMask = YES;
    actionSheet.editAfterSelectThumbnailImage = YES;
    actionSheet.clipRatios = @[GetClipRatio(1,1),GetClipRatio(4,3),GetClipRatio(3,2),GetClipRatio(16,9)];
    
    //设置照片最大选择数
    actionSheet.maxSelectCount = 1;
    
    //    actionSheet.arrSelectedAssets = self.lastSelectAssets;
    MJWeakSelf;
    [actionSheet setSelectImageBlock:^(NSArray<UIImage *> * _Nonnull images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
        
        //        self.lastSelectAssets = [NSMutableArray arrayWithArray:assets];
        //
        weakSelf.selectImages = [NSMutableArray arrayWithArray:images];
        
        weakSelf.model.iconImage = images.firstObject;
        
        [weakSelf.tableView reloadData];
        
        
        
        NSLog(@"%@",images);
        //your codes
    }];
    
    //调用相册
    [actionSheet showPhotoLibrary];
    
}

- (void)selectCommunity{
    
    SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
    
    view.isSingle = YES;
    
    view.array = @[[[SelectedListModel alloc] initWithSid:0 Title:@"东津世纪城"] ,
                   [[SelectedListModel alloc] initWithSid:1 Title:@"民发世界城"]
                   ];
    
    MJWeakSelf;
    view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
        
        [LEEAlert closeWithCompletionBlock:^{
            
            
            weakSelf.model.commityName = [array firstObject].title;
            
            [weakSelf.tableView reloadData];
            
            
            
            
        }];
        
    };
    
    [LEEAlert alert].config
    .LeeTitle(@"选择社区")
    .LeeItemInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeCustomView(view)
    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeClickBackgroundClose(YES)
    .LeeShow();
    
    
}


- (void)selectDetailAdress{
    
    if (self.model.commityName == nil) {
        
        [SVProgressHUD showInfoWithStatus:@"请选择所在社区"];
        
        return;
    }
    

    
    __block NSString *province = nil;
    
    __block NSString *city = nil;
    
    __block NSString *area = nil;
    
    MJWeakSelf
    
    void (^resultBlock)(void) = ^{
        
        NSMutableString *temp = [NSMutableString string];
        
        [temp appendString:province];
         [temp appendString:@"-"];
        [temp appendString:city];
         [temp appendString:@"-"];
        [temp appendString:area];

//
//        NSArray<NSString *> *first = [[NSString stringWithFormat:@"%@%@%@",province,city,area] componentsSeparatedByString:@"年"];
//
//        [temp appendString:first[0]];
//
//        [temp appendString:@"-"];
//
//        first = [first[1] componentsSeparatedByString:@"月"];
//
//        [temp appendString:first[0]];
//
//        [temp appendString:@"-"];
//
//        first = [first[1] componentsSeparatedByString:@"日"];
//
//        [temp appendString:first[0]];
        
        
        weakSelf.model.detailAdress = temp;

        [weakSelf.tableView reloadData];
        
        
        [LEEAlert alert].config
        .LeeTitle(@"结果")
        .LeeContent([NSString stringWithFormat:@"%@ %@ %@" , province ?: @"" , city ?: @"" , area ?: @""])
        .LeeAction(@"好的", nil)
        .LeeShow();
    };
    
    
    NSArray *provinceArray = self.dateArray;
    
    if (provinceArray.count) {
        
        // 获取省数据 并显示
        
        NSMutableArray *array = [NSMutableArray array];
        
        [provinceArray enumerateObjectsUsingBlock:^(NSDictionary *info, NSUInteger idx, BOOL * _Nonnull stop) {
            
            [array addObject:[[SelectedListModel alloc] initWithSid:idx Title:info[@"name"] Context:info]];
        }];
        
        SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
        
        view.isSingle = YES;
        
        view.array = array;
        
        view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
            
            [LEEAlert closeWithCompletionBlock:^{
                
                // 根据选中的省获取市数据 并显示
                
                SelectedListModel *model = array.firstObject;
                
                province = model.title; //设置省
                
                NSArray *cityArray = model.context[@"sub"];
                
                if (cityArray.count) {
                    
                    NSMutableArray *array = [NSMutableArray array];
                    
                    [cityArray enumerateObjectsUsingBlock:^(NSDictionary *info, NSUInteger idx, BOOL * _Nonnull stop) {
                        
                        [array addObject:[[SelectedListModel alloc] initWithSid:idx Title:info[@"name"] Context:info]];
                    }];
                    
                    SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
                    
                    view.isSingle = YES;
                    
                    view.array = array;
                    
                    view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
                        
                        [LEEAlert closeWithCompletionBlock:^{
                            
                            // 根据选中的市获取区数据 并显示
                            
                            SelectedListModel *model = array.firstObject;
                            
                            city = model.title; //设置市
                            
                            NSArray *areaArray = model.context[@"sub"];
                            
                            if (areaArray.count) {
                                
                                NSMutableArray *array = [NSMutableArray array];
                                
                                [areaArray enumerateObjectsUsingBlock:^(NSString *area, NSUInteger idx, BOOL * _Nonnull stop) {
                                    
                                    [array addObject:[[SelectedListModel alloc] initWithSid:idx Title:area]];
                                }];
                                
                                SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
                                
                                view.isSingle = YES;
                                
                                view.array = array;
                                
                                view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
                                    
                                    [LEEAlert closeWithCompletionBlock:^{
                                        
                                        // 获取区数据 并显示最终结果
                                        
                                        SelectedListModel *model = array.firstObject;
                                        
                                        area = model.title; //设置区
                                        
                                        //                                                NSLog(@"%@----",area);
                                        
                                        
                                        if (resultBlock) resultBlock();
                                    }];
                                    
                                };
                                
                                [LEEAlert alert].config
                                .LeeTitle(model.title)
                                .LeeCustomView(view)
                                .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
                                .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
                                .LeeClickBackgroundClose(YES)
                                .LeeShow();
                                
                            } else {
                                
                                if (resultBlock) resultBlock();
                            }
                            
                        }];
                        
                    };
                    
                    [LEEAlert alert].config
                    .LeeTitle(model.title)
                    .LeeCustomView(view)
                    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
                    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
                    .LeeClickBackgroundClose(YES)
                    .LeeShow();
                    
                } else {
                    
                    if (resultBlock) resultBlock();
                }
                
            }];
            
        };
        
        [LEEAlert alert].config
        .LeeTitle(self.model.commityName)
        .LeeCustomView(view)
        .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
        .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
        .LeeClickBackgroundClose(YES)
        .LeeShow();
    }
    
}
    
    
    


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
